package cn.imnaer.rest.controller;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import cn.imnaer.common.factory.JacksonUtil;
import cn.imnaer.service.persistence.model.Recommend;
import cn.imnaer.service.persistence.model.Scenic;
import cn.imnaer.service.rest.recommendcircle.RecommendComponentRestApi;
import cn.imnaer.service.rest.resquest.model.RecommendRequest;
import cn.imnaer.service.rest.view.model.PageInfo;
import cn.imnaer.service.rest.view.model.RecommendView;
import cn.imnaer.service.rest.view.model.Response;
import cn.imnaer.service.rpc.collect.application.CollectComponentRpcApiImpl;
import cn.imnaer.service.rpc.recommend.dao.RecommendDaoImpl;
import cn.imnaer.service.rpc.recommendcircle.impl.RecommendComponentRpcApiImpl;
import cn.imnaer.service.rpc.scenic.ScenicComponentRpcApi;
import cn.imnaer.service.rpc.scenic.dao.ScenicDaoImpl;
import cn.imnaer.service.rpc.scenic.impl.ScenicComponentRpcApiImpl;

@WebService
public class RecommendController implements RecommendComponentRestApi {
	private RecommendComponentRpcApiImpl recommendComponentRpcApiImpl = new RecommendComponentRpcApiImpl();
	private static CollectComponentRpcApiImpl CollectComponentRpcApiImpl = new CollectComponentRpcApiImpl();
	private  ScenicComponentRpcApi  scenicComponentRpcApiImpl=new ScenicComponentRpcApiImpl();
	public Response response;

	public RecommendController() {
		this.response = new Response();
	}

	@Override
	public String Index(String userId, int page) {
		List<RecommendView> notNullList = new ArrayList<>();
		List<RecommendView> list = recommendComponentRpcApiImpl.getRecommendList(page);
		BSONObject matcher = new BasicBSONObject();
		int pageCount = RecommendDaoImpl.getInstance().getPageCount(matcher);
		this.response.setPageCount(pageCount);
		 checkIsCollect(userId, list);
		for (RecommendView recommendView : list) {
			if (recommendView.getRecommendId() != null) {
				notNullList.add(recommendView);
			}
		}
		return this.response.success(notNullList);
	}
	@Override
	public String getCityScenicListByCityCode(String cityCode, String userId, int page) {
		 PageInfo<Scenic> data = scenicComponentRpcApiImpl.getScenicListByCityCode(cityCode, page);
	      List<RecommendView> record=   RecommendView.valueOfScenicList(data.getData());
	        checkIsCollect(userId, record);
	         String json = this.response.success(record,data.getPageCount());
	         return json;
	}
  
/**
 * 
 * 通过Id和type来获取景点列表
 */
@Override
public String getScenicListByRecommendId(String json) {
	String  result="";
  RecommendRequest recommendRequest = JacksonUtil.jsonToBean(json, RecommendRequest.class);
  String  type=recommendRequest.getType();
  String  cityCode=recommendRequest.getRecommendId();
  String  userId=recommendRequest.getUserId();
  int page=recommendRequest.getPage();
  switch (type) {
case RecommendView.CITY:
	result= this.getCityScenicListByCityCode(cityCode, userId, page);
	break;
case RecommendView.SCENIC_GROUP:
	break;

default:
	break;
}
	return result;
}
public static  void checkIsCollect(String userId,List<RecommendView>recommendViewList) {
	List<String> scenicIds = CollectComponentRpcApiImpl.getMyCollectScenicList(userId);
	if (scenicIds.size() > 0) {
		for (RecommendView recommendView : recommendViewList) {
			if ("SCENIC".equals(recommendView.getRecommendType())) {
				String scenicId = recommendView.getRecommendId();
				if (scenicIds.contains(scenicId)) {
					recommendView.setIsCollected(true);
				}
			}
		}
	}
}

@Override
public String getScenicListByScenicGroupId(String cityCode, String userId, int page) {
	// TODO Auto-generated method stub
	return null;
}
}
